package com.ruoyi.oa.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.oa.domain.bo.OaHolidayBo;
import com.ruoyi.oa.domain.vo.OaHolidayVo;
import com.ruoyi.oa.domain.OaHoliday;
import com.ruoyi.oa.mapper.OaHolidayMapper;
import com.ruoyi.oa.service.IOaHolidayService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 假期Service业务层处理
 *
 * @author ruoyi
 * @date 2023-07-12
 */
@RequiredArgsConstructor
@Service
public class OaHolidayServiceImpl implements IOaHolidayService {

    private final OaHolidayMapper baseMapper;

    /**
     * 查询假期
     */
    @Override
    public OaHolidayVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询假期列表
     */
    @Override
    public TableDataInfo<OaHolidayVo> queryPageList(OaHolidayBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<OaHoliday> lqw = buildQueryWrapper(bo);
        Page<OaHolidayVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询假期列表
     */
    @Override
    public List<OaHolidayVo> queryList(OaHolidayBo bo) {
        LambdaQueryWrapper<OaHoliday> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<OaHoliday> buildQueryWrapper(OaHolidayBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<OaHoliday> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getHolidayDate() != null, OaHoliday::getHolidayDate, bo.getHolidayDate());
        lqw.like(StringUtils.isNotBlank(bo.getName()), OaHoliday::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), OaHoliday::getType, bo.getType());
        return lqw;
    }

    /**
     * 新增假期
     */
    @Override
    public Boolean insertByBo(OaHolidayBo bo) {
        OaHoliday add = BeanUtil.toBean(bo, OaHoliday.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改假期
     */
    @Override
    public Boolean updateByBo(OaHolidayBo bo) {
        OaHoliday update = BeanUtil.toBean(bo, OaHoliday.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(OaHoliday entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除假期
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public OaHoliday selectIsHoliday(String date) {
        return baseMapper.selectIsHoliday(date);
    }
}
